package com.purchase.ml.mapper.dealer;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.purchase.ml.bean.req.prod.SysProdQueryDto;
import com.purchase.ml.entity.dealer.SysAddr;
import com.purchase.ml.entity.dealer.SysDealerPruduct;
import com.purchase.ml.entity.prod.SysProduct;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author :zxm
 * @date :2021/3/11 17:09
 * @description :some description
 **/
@Mapper
@Repository
public interface DealerProductMapper  extends BaseMapper<SysDealerPruduct>{

    /**
     * 主经销商/经销商购买商品成功后，保存其商品信息
     * @param product
     */
    @Insert("insert into sys_dealer_pruduct(`product_id`,`state`,`dealer_id`,`stock`,`delivery_day`,`moq`) values(#{product.productId}  ,#{product.state}  ,#{product.dealerId} ,#{product.stock},#{product.deliveryDay} ,#{product.moq}  );")
    int changeMasterDealerStock(@Param("product") SysDealerPruduct product);

    /**
     * 通过经销商id查询出经销商产品信息
     * @param buyer
     * @return
     */
    @Select("select * from sys_dealer_pruduct where dealer_id=#{dealerId} and product_id=#{productId} ")
    SysDealerPruduct findBuyerProductByDealerId(@Param("dealerId") Long buyer,@Param("productId") Long productId);

    /**
     * 增加商户商品库存
     * @param quantity
     * @return
     */
    @Update("update sys_dealer_pruduct set stock=stock+#{quantity} where dealer_id=#{dealerId} and product_id=#{productId}")
    int updateDealerProduct(@Param("dealerId")Long buyer,@Param("productId")Long productId,@Param("quantity") Integer quantity);

    /**
     * 减少主经销商产品库存
     * @param sellers
     * @param productId
     * @param quantity
     * @return
     */
    @Update("update sys_dealer_pruduct set stock=stock-#{quantity} where dealer_id=#{dealerId} and product_id=#{productId} ")
    int reduceMasterStock(@Param("dealerId") Long sellers, @Param("productId") Long productId, @Param("quantity") Integer quantity);

    /**
     * 查询经销商某状态的商品
     * @param state
     * @param dealerId
     * @return
     */
    @Select("select * from sys_dealer_pruduct where `state`=#{state} and dealer_id=#{dealerId} ")
    List<SysDealerPruduct> selectDealerPruduct(@Param("state")Integer state,@Param("dealerId")Long dealerId);
}
